Skip to content

fix(studio): crop and scale resize work in a sealed preview - #8005

Merged
miguel-heygen merged 6 commits into
mainfrom
fix/studio-overlay-geometry-port
Nov 18, 2026
Merged

miguel-heygen merged 6 commits into
mainfrom
fix/studio-overlay-geometry-port

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

What changed

With the preview sealed (VITE_STUDIO_OPAQUE_PREVIEW_ENABLED), Studio's selection is a stand-in element with no styles or box of its own. Two edits read the real element and so went wrong:

  • Crop UI. The crop handles read the element's committed clip-path from the stand-in, which has none, so a cropped element showed no crop and a second drag started from zero. A sealed selection now remembers the frame's inline and computed clip-path (as it already does for translate), and the crop reads that.
  • Crop lift. While a croppable element is selected, Studio hides its clip-path so the cropped-away part shows dimmed. In a sealed preview that rule has to live in the frame, so the editing port gets a crop-lift kind (packages/core/src/runtime/editing/cropLift.ts). It adds or removes the same [data-hf-id] / [id] rule Studio writes today, and answers with the element's clip under the lift. The element's own clip-path stays the saved record.
  • Scale resize. Committing a resize of a GSAP-scaled element divides by the element's original box. With no frame element to read it fell back to 200 px, so the saved scale was wrong by real / 200. The gesture-start read already measures that box; it is now kept per element and used when the frame element can't be read.

With the flag off nothing changes: every new path is taken only for a stand-in.

What I measured

On the devbox, at origin/main with only the new tests applied, all three fail. With the fix: the 21 Studio test files and 4 core test files that import a changed module pass in each of three runs, and tsc passes for core and studio.

fails without the fix: "reads the clip-path the frame renders, as a scriptable frame's does" and "shows the element uncropped in the frame while selected, and crops it again after" in packages/studio/src/components/editor/previewSelection.test.ts, and "scales from the box its gesture started from, not a 200 px fallback" in packages/studio/src/components/editor/previewGestureStartRead.test.ts.

What I did NOT exercise

  • The edit-accuracy bench with the preview sealed. A crop there also needs the style commit to reach a sealed preview, which this PR does not do.
  • A real browser: the tests drive the real editing agent over happy-dom.

Before

main, Studio built with VITE_STUDIO_OPAQUE_PREVIEW_ENABLED=1, headless Chrome at 1600x900, the edit-accuracy fixture with the target cropped by inset(20px 60px 30px 40px), target selected. The preview frame is sealed, so Studio can't read the crop: the target stays cropped and only the resize corners show.

Before: the cropped target stays cropped when selected in a sealed preview

After

This branch, same setup. The crop lift runs in the frame: the target shows uncropped, the cut-away area is dimmed, and the four crop edges sit on the crop.

After: the target shows uncropped with the crop edges on its crop

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 271 (base branch 271), smooth 65 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (0)

@miguel-heygen
miguel-heygen force-pushed the fix/studio-overlay-geometry-port branch 2 times, most recently from 571e1ea to e3c1a03 Compare November 17, 2026 06:25

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested at e3c1a03. The port side is clean: crop-lift is in EDITING_REQUEST_KINDS, PARAM_PARSERS and HANDLERS, the keys are an exact whitelist, lift must be a boolean, handle goes through isHandle, there's no new listener, and nothing beyond computed-styles is exposed. The fails without the fix: tests exist.

Blockers:

  1. Frame-reported size is persisted unbounded (the #8002 failure again). gsapResizeIntercept.ts (~L325) now uses gestureStartLayout(handle), from previewGestureStartRead.ts, as cssW/cssH for a sealed preview. Those values come from the frame and are only checked with Number.isFinite (previewElements.ts:240). started?.width || 200 catches 0 but not -5 or 1e-9, so a reply like that persists a negative or enormous scale into the project. Bound the width and height to (0, MAX_DRAFT_PX], and fall back to 200 otherwise.
  2. A crop lift can stay stuck. In domEditOverlayCrop.ts (liftInFrame), when the lift reply is null (a timeout or a race), the record is deleted even though the frame may have applied the rule. The later restore then finds no record and never un-lifts, so the crop stays hidden in the frame. The lift:false request is also fire-and-forget. Keep the record until the un-lift is confirmed.

Should-fix:

  1. Stale layouts: startLayouts is a module-level Map keyed by handle and never evicted, so a stale layout can carry across iframes and reloads. Use a WeakMap keyed by the stand-in, or clear it on port close.
  2. Unbounded clip-path: clipPath in the reply and frameClipPaths have no length cap. Truncate to MAX_ELEMENT_VALUE and cap the parsed insets.

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Thanks, all four fixed in 51102126. The tests come first, in 0e6164a8.

1. Frame-reported size. Fixed where the reply is accepted. readGestureStart (previewElements.ts) now refuses a layout whose width or height is outside [0, MAX_DRAFT_PX]. measurePreviewGestureStart keeps a start box only when both sides are at least 1 px, so -5, 1e-9 and 1e9 all leave gestureStartLayout null and resize falls back to 200. Test: "keeps no start box for a frame-reported width of %s" in previewGestureStartRead.test.ts.

2. Stuck crop lift. A lift whose reply is lost keeps its record, so deselecting still sends lift:false. Only a reply of lifted:false drops the record. A drop marks the record as dropped and asks the frame up to 3 times, keeping the record until the frame confirms the lift is gone. A new select while one is pending lifts again. Tests: "is kept when the lift's reply is lost..." and "asks again until the frame says the lift is gone..." in the new domEditOverlayCropFrameLift.test.ts.

Should-fix 1. startLayouts is cleared on every measure, so it only ever holds the current gesture's handles.

Should-fix 2. A clipPath reply longer than MAX_ELEMENT_VALUE is ignored. parseCropClipPath treats any side past MAX_DRAFT_PX as a clip the cropper can't represent, so it returns null and the cropper leaves it alone. Test: "ignores a clip-path past the element value cap, and an inset past the draft limit".

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at 5110212. Both blockers are fixed.

  1. Frame-reported size: isLayout in previewElements.ts bounds the frame's width and height to [0, MAX_DRAFT_PX] at the reply parser. previewGestureStartRead keeps a start box only when both sides are at least 1 px, and clears startLayouts on each measure. A bad reply now falls back to 200 instead of being persisted as a negative or enormous scale.
  2. Stuck crop lift: a lift record is kept until the frame confirms lifted === false, with up to 3 un-lift tries. A null lift reply no longer deletes the record. After the retries, dropped lets the next crop start a fresh lift, so nothing stays stuck.

The should-fixes are also in: the reply clipPath is capped at MAX_ELEMENT_VALUE, and the parsed insets are bounded by MAX_DRAFT_PX.

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at ca08a97. This is a refactor only: the stylesheet clip under a lift is read in sheetClipUnder. readElementClipPath is unchanged in behaviour (inline first, then the sheet clip while a lift is live and not dropped, otherwise the computed clip).

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at c7e76b7. This is a rebase onto main. Against ca08a97, the range-diff shows commits 1 and 3-5 identical, and commit 2 differs only in context (the append-seam conflicts). The code is unchanged.

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at a4330d8. This is a one-line test fix, with no source change: the crop-lift test helper now answers the lift it just sent.

@miguel-heygen
miguel-heygen force-pushed the fix/studio-overlay-geometry-port branch from a4330d8 to 0a6cb2b Compare November 17, 2026 13:05

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 0a6cb2b9. This was my first review of this PR, so I reviewed all of it as well as the rebase.

The rebase holds. git range-diff from the last approved head a4330d89 shows commits 3–6 byte-identical. Commits 1–2 differ only in context that main moved underneath them: the watch-geometry kind and the marked-agent import. watch-geometry and crop-lift both appear, in the same order, in EDITING_REQUEST_KINDS, EditingRequests, PARAM_PARSERS and HANDLERS, and in both test lists.

The parser is strict. It requires exactly handle and lift, a valid handle and a real boolean. An extra key, a missing lift, lift:"true", handle:"body", an array or null each get invalid-params.

The frame handler is narrow. It only acts on the element the handle resolves to. It replaces its own earlier rule instead of adding another, and it adds a single clip-path:none!important rule.

Studio treats the reply as untrusted.

  • It checks the reply's type, caps clipPath at MAX_ELEMENT_VALUE, and rejects crop edges above MAX_DRAFT_PX.
  • Un-lifting stops after 3 tries.
  • isLayout requires a finite size within bounds, and a reply must be the same length as its request.

Tests and mutation check. Core editing* passes 237 and the studio suites pass 127. With the crop-lift handler removed, the named "uncropped while selected" test fails.

Nit (non-blocking). At cropLift.ts:12, the selector quoting escapes only " and \. A newline in an id would end the CSS string early. That stays inside the frame's own stylesheet, so it crosses no boundary, but CSS.escape would be tidier here and in Studio's liftSelector.

CI. No failures; some jobs were still running.

— Rames

@miguel-heygen
miguel-heygen force-pushed the fix/studio-overlay-geometry-port branch from 0a6cb2b to 3cc7499 Compare November 17, 2026 13:59

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-stamp at 3cc7499: this is a rebase only, onto main after #8011.

The +/- lines of the patch against its new base are identical to the head I approved at 0a6cb2b. The new head equals merge-tree(origin/main, 0a6cb2b9) with its conflicts resolved. Every conflicted list keeps main's four new entries (audio-meter, document-markup, set-audio-group-attribute, assign-audio-group) followed by this PR's crop-lift:

  • EDITING_REQUEST_KINDS
  • the EditingRequests types
  • PARAM_PARSERS
  • HANDLERS
  • both test expectation arrays

Verified:

  • core editing tests: 245 pass.
  • studio crop-lift and selection tests: 43 pass.
  • CI: 0 failed. Some checks are still pending.

Need from author: nothing.
— Rames

@miguel-heygen
miguel-heygen force-pushed the fix/studio-overlay-geometry-port branch 2 times, most recently from a9fd650 to bb977ac Compare November 17, 2026 22:24

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approve at bb977ac4. I ran git range-diff from my last approved head, 3cc74998, to this one, each against its own merge-base with main.

  • Commits 1, 4, 5 and 6 are unchanged (=).
  • In commits 2 and 3 only the context changed: main's newer gsap-snapshot and motion-path-home entries now sit beside crop-lift in EDITING_REQUEST_KINDS, EditingRequests, PARAM_PARSERS and the agent's HANDLERS, and some import lines in previewGestureStartRead.test.ts moved. Every added line in the PR is the same.

So this is a rebase only, as the body says.

— Rames

Verdict: APPROVE. Needs reuse: no. Over-engineered: no.

@miguel-heygen
miguel-heygen force-pushed the fix/studio-overlay-geometry-port branch from bb977ac to 74353bc Compare November 18, 2026 00:21

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at 74353bc. This is a rebase onto main only. The PR's added and removed lines against its merge base are line-for-line identical to bb977ac, which I approved (298 of 298 match). The entries kept beside the crop-lift show up only as context, so the change I reviewed is unchanged.

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit c569417 Nov 18, 2026
99 of 100 checks passed
@miguel-heygen
miguel-heygen deleted the fix/studio-overlay-geometry-port branch November 18, 2026 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants